﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ComeSeeThis_MVC2.Models
{
    public class GearingRecSearchResultForm
    {
        public IQueryable<GearingRec> GearingRecs { get; private set; }
        public Vehicle Vehicle { get; private set; }
        public Motor Motor { get; private set; }
        public double TransmissionRatio { get; private set; }
        public double AverageFdrExact { get; private set; }
        public double AverageFdrSimilar { get; private set; }
        public double AverageBoth { get; private set;}
        public double Voltage { get; private set; }
        public VehicleType VehicleType { get; private set; }
        public GearingRec GearingRec { get; private set; }
        public IQueryable<Tire> Tires { get; private set; }
        public int TireId { get; private set; }

        public GearingRecSearchResultForm(GearingRec gearingRec, int cellCount, float transmissionRatio)
        {
            gearingRec.CellCount = cellCount;
            gearingRec.CellType = new GearingRecRepository().GetCellType(int.Parse(gearingRec.CellTypeId.ToString()));
            Voltage = cellCount*gearingRec.CellType.CellVoltage;

            TransmissionRatio = transmissionRatio;
            if (gearingRec.VehicleId != null) Vehicle = new VehicleRepository().GetVehicle(int.Parse(gearingRec.VehicleId.ToString()));

            Motor = new MotorRepository().GetMotor(gearingRec.MotorId);
            VehicleType = new VehicleTypeRepository().GetVehicleType(gearingRec.VehicleTypeId);
            //Tires = new SelectList(new TireRepository().GetAllTiresByVehicleType(gearingRec.VehicleTypeId), "TireDiameter", "TireName");
            Tires = new TireRepository().GetAllTiresByVehicleType(gearingRec.VehicleTypeId);

            // Get results
            GearingRecs = new GearingRecRepository().SearchGearingRecs(gearingRec);

            // Get the average FDRs
            var exactCount = 0;
            var similarCount = 0;
            double exactSum = 0;
            double similarSum = 0;

            foreach (var rec in GearingRecs)
            {
                if (rec.MotorId == gearingRec.MotorId && rec.VehicleId == gearingRec.VehicleId)
                {
                    exactCount++;
                    exactSum += rec.FDR;
                }
                else
                {
                    similarCount++;
                    similarSum += rec.FDR;
                }
            }
            if (exactCount > 0) AverageFdrExact = Math.Round((exactSum/exactCount), 2);
            if (similarCount > 0) AverageFdrSimilar = Math.Round((similarSum/similarCount), 2);
            
            GearingRec = gearingRec;

            //AverageBoth = Math.Round((AverageFdrExact + AverageFdrSimilar)/2, 2);
            AverageBoth = Math.Round((exactSum + similarSum) / (exactCount + similarCount), 2);
        }
    }
}